package com.handydata.antifraud.service.impl;

import com.handydata.antifraud.bo.appcategorybasedata.AppCategoryBasedataBeanBO;
import com.handydata.antifraud.bo.appcategorybasedata.AppCategoryBasedataReqBO;
import com.handydata.antifraud.bo.appcategorybasedata.AppCategoryBasedataResBO;
import com.handydata.antifraud.common.exception.AppCategoryBasedataExCode;
import com.handydata.antifraud.dao.AppCategoryBasedataDao;
import com.handydata.antifraud.entity.AppCategoryBasedataDO;
import com.handydata.antifraud.service.AppCategoryBasedataService;
import com.handydata.common.convert.CommonBeanConvertor;
import com.handydata.common.exception.SysException;
import com.handydata.common.service.AbsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("appCategoryBasedataService")
public class AppCategoryBasedataServiceImpl extends AbsService implements AppCategoryBasedataService {
	
	private static final Logger logger = LoggerFactory.getLogger(AppCategoryBasedataServiceImpl.class);
	
	@Autowired
	private AppCategoryBasedataDao appCategoryBasedataDao;

	@Transactional
	@Override
	public AppCategoryBasedataResBO createAppCategoryBasedata(AppCategoryBasedataReqBO req) throws SysException{
	    AppCategoryBasedataResBO res = new AppCategoryBasedataResBO();
		try {
			AppCategoryBasedataDO entity = CommonBeanConvertor.convert(req, AppCategoryBasedataDO.class);
			long status = appCategoryBasedataDao.insert(entity);
			if(status == 0) {
				res.setExCode(AppCategoryBasedataExCode.SYS_NO_ENTITY_CREATED);
			} else {
				res.setExCode(AppCategoryBasedataExCode.SUCCESS);
			}
 			
		} catch (Exception e) {
			logger.error("Create new entity {} get into error", req, e);
			throw new SysException(AppCategoryBasedataExCode.SYS_CREATE_ENTITY_ERROR);
		}
		
		return res;
	}

	@Transactional
	@Override
	public AppCategoryBasedataResBO modifyAppCategoryBasedata(AppCategoryBasedataReqBO req) throws SysException{
	    AppCategoryBasedataResBO res = new AppCategoryBasedataResBO();
		try {
			AppCategoryBasedataDO condition = CommonBeanConvertor.convert(req, AppCategoryBasedataDO.class);
			long status = appCategoryBasedataDao.update(condition);
			if(status == 0) {
				res.setExCode(AppCategoryBasedataExCode.SYS_NO_ENTITY_MODIFIED);
			} else {
				res.setExCode(AppCategoryBasedataExCode.SUCCESS);
			}
		} catch (Exception e) {
			logger.error("Modify entity {} get into error", req, e);
			throw new SysException(AppCategoryBasedataExCode.SYS_MODIFY_ENTITY_ERROR);
		} 
		return res;
	}

	@Transactional
	@Override
	public AppCategoryBasedataResBO deleteAppCategoryBasedata(AppCategoryBasedataReqBO req) throws SysException{
	    AppCategoryBasedataResBO res = new AppCategoryBasedataResBO();
		
		try {
			AppCategoryBasedataDO condition = CommonBeanConvertor.convert(req, AppCategoryBasedataDO.class);
			long status = appCategoryBasedataDao.delete(condition);
			if(status == 0) {
				res.setExCode(AppCategoryBasedataExCode.SYS_NO_ENTITY_DELETED);
			} else {
				res.setExCode(AppCategoryBasedataExCode.SUCCESS);
			}
		} catch (Exception e) {
			logger.error("Delete entity {} get into error", req, e);
			throw new SysException(AppCategoryBasedataExCode.SYS_DELETE_ENTITY_ERROR);
		}
		return res;
	}

	@Override
	public AppCategoryBasedataResBO queryAppCategoryBasedata(AppCategoryBasedataReqBO req) throws SysException{
		AppCategoryBasedataDO condition = CommonBeanConvertor.convert(req, AppCategoryBasedataDO.class);
		AppCategoryBasedataResBO res;
		
		try {
			List<AppCategoryBasedataDO> entities = appCategoryBasedataDao.query(condition);
			res = CommonBeanConvertor.convert(entities, AppCategoryBasedataResBO.class, AppCategoryBasedataBeanBO.class);
		} catch(Exception e) {
			logger.error("Query entity {} get into error", req, e);
			throw new SysException(AppCategoryBasedataExCode.SYS_QUERY_ENTITY_ERROR);
		}
		return res;
	}
	
}
